<?php

   class Dbc
   {

       private $host;

       private $user;

      private $password;

      private $charset;

      private $database;

      /**
    17      * 新建数据库连接对象，测试数据库连接
    18      *
    19      * @param string $host
    20      * @param string $user
    21      * @param string $password
    22      * @param string $charset
    23      * @param string $database
    24      */
      function __construct($host, $user, $password, $charset, $database)
      {
                 $link = mysqli_connect($host, $user, $password) or die('数据库连接失败<br />ERROR ' . mysqli_connect_errno() . ':' . mysqli_connect_error());
          $char = mysqli_set_charset($link, $charset) or die('charset设置错误，请输入正确的字符集名称<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link));
          $db = mysqli_select_db($link, $database) or die('未找到数据库，请输入正确的数据库名称<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link));
          $this->host = $host;
          $this->user = $user;
          $this->password = $password;
          $this->charset = $charset;
          $this->database = $database;
          mysqli_close($link);
      }

      /**
    39      * 连接数据库
    40      *
    41      * @param string $host
    42      * @param string $user
    43      * @param string $password
    44      * @param string $charset
    45      * @param string $database
    46      * @return object 连接标识符
    47      */
      private function connect($host, $user, $password, $charset, $database)
      {
                 $link = mysqli_connect($this->host, $this->user, $this->password);
          mysqli_set_charset($link, $this->charset);
          mysqli_select_db($link, $this->database);
          return $link;
      }

      /**
    57      * 增加数据
    58      *
    59      * @param array $data
    60      * @param string $table
    61      * @return boolean
    62      */
      public function insert($data, $table)
      {
            $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
          $keys = join(',', array_keys($data));
          $vals = "'" . join("','", array_values($data)) . "'";
          $query = "INSERT INTO {$table}({$keys}) VALUES({$vals})";
          $result = mysqli_query($link, $query) or die('插入数据出错，请检查！<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link));
          if ($result) {
              $id = mysqli_insert_id($link);
          } else {
                $id = false;
          }
         mysqli_close($link);
          return $id;
      }

      /**
    80      * 删除数据
    81      *
    82      * @param string $table
    83      * @param string $where
    84      * @return boolean
    85      */
      public function delete($table, $where = null)
      {
         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
         $where = $where ? ' WHERE ' . $where : '';
         $query = "DELETE FROM {$table}{$where}";
         $result = mysqli_query($link, $query) or die('删除数据出错，请检查！<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link));
         if ($result) {
             $row = mysqli_affected_rows($link);
         } else {
             $row = false;
         }
         mysqli_close($link);
         return $row;
     }

     public function query($sql){
     	$link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
     	$link->query($sql);
     }

     /**
    102      * 修改数据
    103      *
    104      * @param array $data
    105      * @param string $table
    106      * @param string $where
    107      * @return boolean
    108      */
     //$data = array('coin_addr'=>'0xdkjhajkdwjhaw'),$table='ld_user_info',$where ="id=$_SESSION['id']" ;
     public function update($data, $table, $where = null)
     {
         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
         $set = '';
         foreach ($data as $key => $val) {
             $set .= "{$key}='{$val}',";
         }
         $set = trim($set, ',');
         $where = $where ? ' WHERE ' . $where : '';
         $query = "UPDATE {$table} SET {$set}{$where}";
         //return $query;
         $result = mysqli_query($link, $query) or die('数据修改错误，请检查！<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link));
         if ($result) {
             $row = mysqli_affected_rows($link);
         } else {
            $row = false;
         }
         mysqli_close($link);
         return $row;
     }

     /**
    130      * 查询指定记录
    131      *
    132      * @param string $query
    133      * @param string $result_type
    134      * @return array|boolean
    135      */
     public function select_one($query, $result_type = MYSQLI_ASSOC)
     {
         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
         $result = mysqli_query($link, $query) or die('查询语句错误，请检查！<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link));
         if ($result && mysqli_num_rows($result) > 0) {
             $row = mysqli_fetch_array($result, $result_type);
         } else {
             $row = false;
         }
         mysqli_free_result($result);
         mysqli_close($link);
         return $row;
     }

     /**
    151      * 查询所有记录
    152      *
    153      * @param string $query
    154      * @param string $result_type
    155      * @return array|boolean
    156      */
     public function select_all($query, $result_type = MYSQLI_ASSOC)
     {
         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
         // $query = "SELECT * FROM {$table}";
         $result = mysqli_query($link, $query) or die('查询语句错误，请检查！<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link));
         if ($result && mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result, $result_type)) {
                 $rows[] = $row;
             }
         } else {
             $rows = false;
         }
         mysqli_free_result($result);
         mysqli_close($link);
         return $rows;
     }

     /**
    175      * 得到表中记录数
    176      *
    177      * @param string $table
    178      * @return number|boolean
    179      */
     public function get_total_rows($table)
     {
         $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database);
        $query = "SELECT COUNT(*) AS totalRows FROM {$table}";
         $result = mysqli_query($link, $query);
         if ($result && mysqli_num_rows($result) == 1) {
             $row = mysqli_fetch_assoc($result);
         } else {
             $row['totalRows'] = false;
         }
         mysqli_close($link);
         return $row['totalRows'];
     }
 }